/*     */ package com.finir.fakturisanje.util;
/*     */ 
/*     */ import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
/*     */ 
/*     */ public class HibernateUtil
/*     */ {
/*  22 */   private static SessionFactory sessionFactory = null;
/*  23 */   private static Session session = null;
/*  24 */   private static Transaction transaction = null;
/*     */ 
/*     */   static
/*     */   {
/*     */     try
/*     */     {
/*  53 */       if (sessionFactory == null) {
/*  54 */       sessionFactory = new AnnotationConfiguration().configure(
/*  55 */         "/META-INF/hibernate.cfg.xml").buildSessionFactory();
}
/*     */     }
/*     */     catch (Throwable ex)
/*     */     {
/*  59 */       System.err.println("Initial SessionFactory creation failed." + ex);
/*  60 */       throw new ExceptionInInitializerError(ex);
/*     */     }
/*     */   }
/*     */ 
/*     */   public static Session getSession()
/*     */   {
/*  28 */     if (session == null) {
/*  29 */       session = sessionFactory.openSession();
/*  30 */       return session;
/*     */     }
/*  32 */     return session;
/*     */   }
/*     */ 
/*     */   public static void setSession(Session session)
/*     */   {
/*  37 */     HibernateUtil.session = session;
/*     */   }
/*     */ 
/*     */   public static Transaction getTransaction() {
/*  41 */     return transaction;
/*     */   }
/*     */ 
/*     */   public static void setTransaction(Transaction tx) {
/*  45 */     transaction = tx;
/*     */   }
/*     */ 
/*     */   public static SessionFactory getSessionFactory()
/*     */   {
/*  65 */     return sessionFactory;
/*     */   }
/*     */ 
/*     */   public static void beginTransaction()
/*     */   {
/*  70 */     setTransaction(getSession().beginTransaction());
/*     */   }
/*     */ 
/*     */   public void flush()
/*     */   {
/*  75 */     if (getSession() != null)
/*  76 */       getSession().flush();
/*     */   }
/*     */ 
/*     */   public void clear()
/*     */   {
/*  82 */     if (getSession() != null)
/*  83 */       getSession().clear();
/*     */   }
/*     */ 
/*     */   public static void closeSession()
/*     */   {
/*     */     try
/*     */     {
/*  90 */       Session s = getSession();
/*  91 */       setSession(null);
/*  92 */       if ((s == null) || (!(s.isOpen()))) return;
/*  93 */       s.close();
/*     */     } catch (HibernateException ex) {
/*  95 */       ex.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void commitTransaction() {
/* 100 */     Transaction tx = getTransaction();
/*     */     try {
/* 102 */       if ((tx != null) && (!(tx.wasCommitted())) && (!(tx.wasRolledBack())))
/* 103 */         tx.commit();
/* 104 */       setTransaction(null);
/*     */     } catch (HibernateException ex) {
/* 106 */       rollbackTransaction();
/* 107 */       ex.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void rollbackTransaction()
/*     */   {
/* 113 */     Transaction tx = getTransaction();
/*     */     try {
/* 115 */       setTransaction(null);
/* 116 */       if ((tx == null) || (tx.wasCommitted()) || (tx.wasRolledBack()))
/*     */        tx.rollback();
/*     */     }
/*     */     catch (HibernateException ex) {
/* 121 */       ex.printStackTrace();
/*     */ 
/* 124 */       closeSession(); } finally { closeSession();
/*     */     }
/*     */   }
/*     */ }

